package com.pkx.guide.record.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pkx.guide.common.util.StringUtils;
import com.pkx.guide.record.entity.ModifyHistoryDTO;
import com.pkx.guide.record.entity.TRecordGuidePublish;
import com.pkx.guide.record.mapper.TRecordGuidePublishMapper;
import com.pkx.guide.record.service.TRecordGuidePublishService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 诱导屏发布记录表 服务实现类
 *
 * @author sunsy
 * @since 2023-11-14
 */
@Service
public class TRecordGuidePublishServiceImpl extends ServiceImpl<TRecordGuidePublishMapper, TRecordGuidePublish> implements TRecordGuidePublishService {

    /**
     * 分页列表
     *
     * @param tRecordGuidePublish
     * @return
     */
    @Override
    public IPage<TRecordGuidePublish> pageList(Page page, TRecordGuidePublish tRecordGuidePublish) {
        LambdaQueryWrapper<TRecordGuidePublish> lambdaQueryWrapper = new QueryWrapper<TRecordGuidePublish>().lambda();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(tRecordGuidePublish.getGuidNo()), TRecordGuidePublish::getGuidNo, tRecordGuidePublish.getGuidNo());
        lambdaQueryWrapper.eq(tRecordGuidePublish.getSchemeId() != null, TRecordGuidePublish::getSchemeId, tRecordGuidePublish.getSchemeId());
        lambdaQueryWrapper.eq(tRecordGuidePublish.getModeType() != null, TRecordGuidePublish::getModeType, tRecordGuidePublish.getModeType());
        lambdaQueryWrapper.apply(StringUtils.isNotEmpty(tRecordGuidePublish.getQueryStartTime()), "publish_time >= '" + tRecordGuidePublish.getQueryStartTime() + "'");
        lambdaQueryWrapper.apply(StringUtils.isNotEmpty(tRecordGuidePublish.getQueryEndTime()), "publish_time <= '" + tRecordGuidePublish.getQueryEndTime() + "'");
        lambdaQueryWrapper.eq(TRecordGuidePublish::getDelFlg, 0);
        lambdaQueryWrapper.orderByDesc(TRecordGuidePublish::getCreateTime);
        return page(page, lambdaQueryWrapper);
    }

    /**
     * 列表查询
     *
     * @param tRecordGuidePublish
     * @return
     */
    @Override
    public List<TRecordGuidePublish> listByEntity(TRecordGuidePublish tRecordGuidePublish) {
        LambdaQueryWrapper<TRecordGuidePublish> lambdaQueryWrapper = new QueryWrapper<TRecordGuidePublish>().lambda();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(tRecordGuidePublish.getGuidNo()), TRecordGuidePublish::getGuidNo, tRecordGuidePublish.getGuidNo());
        lambdaQueryWrapper.eq(tRecordGuidePublish.getSchemeId() != null, TRecordGuidePublish::getSchemeId, tRecordGuidePublish.getSchemeId());
        lambdaQueryWrapper.eq(tRecordGuidePublish.getModeType() != null, TRecordGuidePublish::getModeType, tRecordGuidePublish.getModeType());
        lambdaQueryWrapper.apply(StringUtils.isNotEmpty(tRecordGuidePublish.getQueryStartTime()), "publish_time >= '" + tRecordGuidePublish.getQueryStartTime() + "'");
        lambdaQueryWrapper.apply(StringUtils.isNotEmpty(tRecordGuidePublish.getQueryEndTime()), "publish_time <= '" + tRecordGuidePublish.getQueryEndTime() + "'");
        lambdaQueryWrapper.eq(TRecordGuidePublish::getDelFlg, 0);
        lambdaQueryWrapper.orderByDesc(TRecordGuidePublish::getCreateTime);
        return list(lambdaQueryWrapper);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public TRecordGuidePublish selectById(Integer id) {
        return getById(id);
    }

    /**
     * 保存
     *
     * @param tRecordGuidePublish
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveTRecordGuidePublish(TRecordGuidePublish tRecordGuidePublish) {
        return save(tRecordGuidePublish);
    }

    /**
     * 修改
     *
     * @param tRecordGuidePublish
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modify(TRecordGuidePublish tRecordGuidePublish) {
        return updateById(tRecordGuidePublish);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Integer id) {
        if (id == null){
            return false;
        }
        LambdaUpdateWrapper<TRecordGuidePublish> lambdaUpdateWrapper = new UpdateWrapper<TRecordGuidePublish>().lambda()
            .eq(TRecordGuidePublish::getId, id)
            .set(TRecordGuidePublish::getDelFlg, 1);
        return update(lambdaUpdateWrapper);
    }

    @Override
    public Boolean modifyByPathParam(ModifyHistoryDTO modifyHistoryDTO) {
        LambdaUpdateWrapper<TRecordGuidePublish> set = new UpdateWrapper<TRecordGuidePublish>().lambda()
                .eq(TRecordGuidePublish::getId, modifyHistoryDTO.getId())
                .set(TRecordGuidePublish::getPublishDetail, modifyHistoryDTO.getPublishDetail());
        return update(set);
    }
}
